﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using appDataContracts;

namespace appServices
{
    public static class Library
    {
        // Méthodes spécifiques pour alléger le code métier

        public static List<SerializedCatalog> getCatalogsOf(PRODUCT product)
        {
            try
            {
                List<SerializedCatalog> listCatalogs = new List<SerializedCatalog>();

                foreach (CATALOG catalog in product.CATALOG)
                {
                    listCatalogs.Add(new SerializedCatalog(catalog.CATALOGID, catalog.SUPPLIERID,
                        catalog.CATALOGDESCRIPTION, catalog.CATALOGNAME, catalog.CATALOGIMAGE));
                }

                return listCatalogs;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return new List<SerializedCatalog>();
            }
        }

        public static List<SerializedContain> getArticlesOf(ORDER order)
        {
            try
            {
                List<SerializedContain> listArticles = new List<SerializedContain>();

                foreach (CONTAIN article in order.CONTAIN)
                {
                    listArticles.Add(Library.getSerializedContain(article));
                }

                return listArticles;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return new List<SerializedContain>();
            }
        }

        public static List<SerializedProduct> getProductsOf(CATALOG catalog)
        {
            try
            {
                List<SerializedProduct> listProducts = new List<SerializedProduct>();

                foreach (PRODUCT product in catalog.PRODUCT)
                {
                    listProducts.Add(Library.getSerializedProduct(product));
                }

                return listProducts;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return new List<SerializedProduct>();
            }
        }

        public static List<SerializedProduct> getProductsOf(ORDER order)
        {
            try
            {
                List<SerializedProduct> listProducts = new List<SerializedProduct>();

                foreach (CONTAIN article in order.CONTAIN)
                {
                    PRODUCT product = (PRODUCT)Database.getInstance().getModel().PRODUCT.
                        First(prod => prod.PRODUCTID == article.PRODUCTID);

                    listProducts.Add(Library.getSerializedProduct(product));
                }

                return listProducts;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return new List<SerializedProduct>();
            }
        }

        // Méthodes pour renvoyer les objets sérialisés

        public static SerializedOrder getSerializedOrder(ORDER order)
        {
            try
            {
                return new SerializedOrder(
                    order.ORDERID,
                    order.CLIENTID,
                    order.ORDERDATE,
                    order.ORDERPAID,
                    order.ORDERTOTAL,
                    order.ORDERCARDOWNER,
                    order.ORDERCARDNUMBER,
                    order.ORDERCARDEXPIRATIONDATE,
                    order.ORDERCARDSECURENUMBER,
                    order.ORDERCARDTYPE,
                    getArticlesOf(order));
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public static SerializedCatalog getSerializedCatalog(CATALOG catalog)
        {
            try
            {
                return new SerializedCatalog(
                    catalog.CATALOGID,
                    catalog.SUPPLIERID,
                    catalog.CATALOGDESCRIPTION,
                    catalog.CATALOGNAME,
                    catalog.CATALOGIMAGE);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public static SerializedClient getSerializedClient(CLIENT client)
        {
            try
            {
                return new SerializedClient(
                    client.CLIENTID,
                    client.CLIENTLASTNAME,
                    client.CLIENTFIRSTNAME,
                    client.CLIENTADRESS,
                    client.CLIENTZIP,
                    client.CLIENTCITY,
                    client.CLIENTPHONE,
                    client.CLIENTMAIL,
                    client.CLIENTCOUNTRY,
                    client.CLIENTPASSWORD);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public static SerializedProduct getSerializedProduct(PRODUCT product)
        {
            try
            {
                return new SerializedProduct(
                    product.PRODUCTID,
                    product.PRODUCTNAME,
                    product.PRODUCTPRICE,
                    product.PRODUCTDESCRIPTION,
                    product.PRODUCTIMAGE,
                    product.SUPPLIERID);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public static SerializedSupplier getSerializedSupplier(SUPPLIER supplier)
        {
            try
            {
                return new SerializedSupplier(
                    supplier.SUPPLIERID,
                    supplier.SUPPLIERNAME,
                    supplier.SUPPLIERADRESS,
                    supplier.SUPPLIERZIP,
                    supplier.SUPPLIERCITY,
                    supplier.SUPPLIERCOUNTRY,
                    supplier.SUPPLIERMAIL,
                    supplier.SUPPLIERPHONE,
                    supplier.SUPPLIERPASSWORD,
                    supplier.SUPPLIERDATELIMIT);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public static SerializedAdmin getSerializedAdmin(ADMIN admin)
        {
            try
            {
                return new SerializedAdmin(
                    admin.ADMINID,
                    admin.ADMINMAIL,
                    admin.ADMINPASSWORD
                    );
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public static SerializedContain getSerializedContain(CONTAIN contain)
        {
            try
            {
                return new SerializedContain(
                    contain.PRODUCTID,
                    contain.ORDERID,
                    contain.CONTAINSENT,
                    contain.CONTAINQUANTITY
                    );
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }
}
